import os
import nltk
from nltk.chunk import RegexpParser
from nltk.tree import Tree

global dir 
dir = os.getcwd()

# Fungsi untuk melakukan POS tag pada kalimat
# sentence : kalimat yang akan di-tag
# return : kalimat yang telah dilabeli pos tag
def tagSentence(sentence):
	words = nltk.word_tokenize(str(sentence))
	tags = nltk.pos_tag(words)
	return tags

# Fungsi untuk membaca daftar grammar dari sebuah file
# infile : path ke file grammar
# return : list grammar
def loadGrammar(infile):
	grammar = ""
	
	f = open(infile, "r")
	
	for pattern in f:
		pattern = pattern.strip()
		
		grammar = grammar + pattern + "\n"
	
	f.close()
	
	return grammar

# Fungsi untuk melakukan phrase chunking ke kalimat yang telah di labeli pos tag
# sentence_tagged : kalimat yang telah dilabeli pos tag
# return : parse tree yang dihasilkan
def chunkPhrase(sentence_tagged):
	grammar = loadGrammar("grammar.txt")
	
	Chunker = RegexpParser(grammar)
	
	parse_tree = Chunker.parse(sentence_tagged)
	return parse_tree

# Fungsi untuk mengekstrak semua tipe frase dari sebuah parse tree
# parse_tree : pohon struktur frase
# return : list frase
def extractPhrase(parse_tree):
	i = 0
	total = len(parse_tree)
	
	phrase_type = ["NP", "VP", "PP", "AP", "ADVP"]
	phrase = []
	
	while(i < total):
		if(isinstance(parse_tree[i], Tree)):
			type = parse_tree[i].node
			
			if(type in phrase_type):
				words = parse_tree[i].leaves()
				
				temp = ""
				for word in words:
					temp = temp + word[0] + " "
				
				phrase.append([type, temp.strip()])
			
			if(parse_tree[i].height > 3):
				phrase = phrase + extractPhrase(parse_tree[i])
		
		i += 1
	
	return phrase

# Fungsi untuk mengekstrak semua NP dari sebuah parse tree
# parse_tree : pohon struktur frase
# return : list NP
def extractNounPhrase(parse_tree):
	i = 0
	total = len(parse_tree)
	
	phrase = []
	
	while(i < total):
		if(isinstance(parse_tree[i], Tree)):
			type = parse_tree[i].node
			
			if(type == "NP"):
				words = parse_tree[i].leaves()
				
				temp = ""
				for word in words:
					temp = temp + word[0] + " "
				
				phrase.append(temp.strip())
			
			if(parse_tree[i].height > 3):
				phrase = phrase + extractNounPhrase(parse_tree[i])
		
		i += 1
	
	NPList = []
	
	for NP in phrase:
		if(not(NP in NPList)):
			NPList.append(NP)
	
	return NPList

# Fungsi untuk mengekstrak semua VP dari sebuah parse tree
# parse_tree : pohon struktur frase
# return : list VP
def extractVerbPhrase(parse_tree):
	i = 0
	total = len(parse_tree)
	
	phrase = []
	
	while(i < total):
		if(isinstance(parse_tree[i], Tree)):
			type = parse_tree[i].node
			
			if(type == "VP"):
				words = parse_tree[i].leaves()
				
				temp = ""
				for word in words:
					temp = temp + word[0] + " "
				
				phrase.append(temp.strip())
			
			if(parse_tree[i].height > 3):
				phrase = phrase + extractVerbPhrase(parse_tree[i])
		
		i += 1
	
	VPList = []
	
	for VP in phrase:
		if(not(VP in VPList)):
			VPList.append(VP)
	
	return VPList

# print extractNounPhrase(chunkPhrase(tagSentence("It was still unclear whether the International Atomic Energy Agency (IAEA) team, headed by chief inspector Herman Nackaerts, would inspect nuclear sites or just discuss with Iranian officials possible military dimensions of Iran's nuclear programme.")))
